Skip to content

💥 Rename regex to pattern to provide compatibility with Pydantic V2 #1231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

stickM4N
Copy link

@stickM4N stickM4N commented Dec 1, 2024

I faced this issue with Pydantic V2 and there is a simple workaround to it by passing the pattern Field parameter as schema extra:

Ex:

class MyClass(SQLModel)
    id: str = Field(unique=True, schema_extra={'pattern': r"^\d{5}$"})

This should be labeled as breaking
I don't know if this issue was already fixed in another branch, I saw there was a temporary PR already made but closed.

@stickM4N
Copy link
Author

@tiangolo @alejsdev or some other maintainer... could you please review this issue?

@svlandeg svlandeg changed the title Add compatibility for Pydantic V2 regex (renamed param from regex to … 💥 Rename regex to param to provide compatibility with Pydantic V2 Feb 20, 2025
@svlandeg
Copy link
Member

Thanks for the PR! We're going through the backlog of PRs right now, and will get back to you once we've had the time to review this and consider the (breaking) change 🙏

@stickM4N
Copy link
Author

stickM4N commented Feb 22, 2025

@svlandeg Nice to hear you are taking action on this matter. There is remark, the parameter the was renamed between Pydantic v1 and v2 is regex-> pattern, not as listed in the PR.

The suggestion I made in the PR was to keep both versions compatible when upgrading, but there is an alternative, which is not managing the change between them and just provide the proper type hints.

If you want me to update the PR just let me know, IDK which approach is more adequate for the project.

@YuriiMotov
Copy link

@stickM4N, could you please clarify why this should be treated as breaking change?

Apart from that, should we add a test for new parameter?

@stickM4N stickM4N changed the title 💥 Rename regex to param to provide compatibility with Pydantic V2 💥 Rename regex to pattern to provide compatibility with Pydantic V2 Aug 8, 2025
@stickM4N
Copy link
Author

stickM4N commented Aug 8, 2025

Hi @YuriiMotov, nice to hear from you again.
Sorry for not have given a better description for the problem, but here it is:

from sqlmodel import SQLModel, Field

class MyClass(SQLModel):
    id: str = Field(unique=True, pattern=r"^\d{5}$")

This code raises TypeError: Field() got an unexpected keyword argument 'pattern' which is the intended keyword for pydantic.v2.FieldInfo, differet from regex for pydantic.v1.FieldInfo
The aim of this PR is to make them compatible and have the same logic for both version sqlmodel-wise

I have other proposition that is more comfortable to work with in my opinion, but its more of a change... and its to convert schema_extra param to **schema_extra, so the input from sqlmodel.Field is more flexible, should not have impacts in other places and it add flexibility to implementations and simpler way to migrate from pydantic.Field to sqlmodel.Field

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants